package com.healthcloud.service.impl;

import com.google.common.base.Joiner;
import com.healthcloud.dao.HlHospitalDao;
import com.healthcloud.dao.HospitalDepartmentMapper;
import com.healthcloud.entity.HlHospitalEntity;
import com.healthcloud.entity.HospitalDepartment;
import com.healthcloud.entity.SysOrganiEntity;
import com.healthcloud.service.HlHospitalService;
import com.healthcloud.service.hospital.SysOrganiService;
import com.healthcloud.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Transactional
@Service("hlHospitalService")
public class HlHospitalServiceImpl implements HlHospitalService {
	@Autowired
	private HlHospitalDao hlHospitalDao;

	@Autowired
	private HospitalDepartmentMapper hospitalDepartmentMapper;

	@Autowired
	private SysOrganiService sysOrganiService;

	@Override
	public HlHospitalEntity queryObject(Long id){
		return hlHospitalDao.queryObject(id);
	}

	@Override
	public List<HlHospitalEntity> queryList(HlHospitalEntity bean, PageParam pageParam){
//		bean.setHosType(HospitalUtil.HOS_TYPE_HOSP);
		return hlHospitalDao.queryList(bean, pageParam);
	}

	@Override
	public int queryTotal(HlHospitalEntity bean){
		return hlHospitalDao.queryTotal(bean);
	}

	@Override
	public void save(HlHospitalEntity hlHospital){
		EntityUtils.initDateTime(hlHospital);
		hlHospital.setHosType(HospitalUtil.HOS_TYPE_HOSP);
		hlHospitalDao.save(hlHospital);
	}

	@Override
	public void update(HlHospitalEntity hlHospital){
		hlHospitalDao.update(hlHospital);
	}

	@Override
	public void delete(Long id){
		hlHospitalDao.delete(id);
	}

	@Override
	public void deleteBatch(Long[] ids){
		hlHospitalDao.deleteBatch(ids);
	}

	@Override
	public void save(HlHospitalEntity entity, List<HospitalDepartment> hdList) {
		//新增医院信息
		save(entity);
		//新增医院与科室关联信息
		int hospId = new Long(entity.getId()).intValue();
		for(int i=0;i<hdList.size();i++){
			HospitalDepartment hd = hdList.get(i);
			hd.setHosId(hospId);
			hd.setCreatedTime(new Date());
			hd.setUpdatedTime(new Date());
			hospitalDepartmentMapper.save(hd);
		}

	}

	@Override
	public void update(HlHospitalEntity entity, List<HospitalDepartment> hdList) {
		//更新医院信息
		entity.setUpdatedTime(new Date());
		entity.setHosType(HospitalUtil.HOS_TYPE_HOSP);
		hlHospitalDao.update(entity);
		//根据医院id查询医院与科室关联信息 oldHdList
		HospitalDepartment oleHd = new HospitalDepartment();
		oleHd.setHosId(entity.getId().intValue());
		List<HospitalDepartment> hdList_old = hospitalDepartmentMapper.findByEntityAll(oleHd);
		//更新医院与科室关联信息
		int hospId = new Long(entity.getId()).intValue();
		List<HospitalDepartment> hdList_delete = new ArrayList<>();//要删除的 医院与科室关联信息 数据
		List<HospitalDepartment> hdList_update = new ArrayList<>();//要更新的 医院与科室关联信息 数据
		List<HospitalDepartment> hdList_add = new ArrayList<>();//要新增的 医院与科室关联信息 数据

		for (int i = 0; i < hdList.size(); i++) {
			HospitalDepartment hd_new = hdList.get(i);
			boolean b = false;
			for (int j = 0; j < hdList_old.size(); j++) {
				if (hd_new.getDepId().equals(hdList_old.get(j).getDepId())) {
					hdList_update.add(hd_new);
					b = true;
					break;
				}
			}
			if (!b) {
				hdList_add.add(hd_new);
			}
		}

		for (int i = 0; i < hdList_old.size(); i++) {
			HospitalDepartment hd_old = hdList_old.get(i);
			boolean b = false;
			for (int j = 0; j < hdList.size(); j++) {
				if (hd_old.getDepId().equals(hdList.get(j).getDepId())) {
					b = true;
					break;
				}
			}
			if (!b) {
				hdList_delete.add(hd_old);
			}
		}

		//删除医院与科室关联信息
		if(hdList_delete.size()!=0){
			Integer[] hdId_delete = new Integer[hdList_delete.size()];
			Long[] hdId_delete_long = new Long[hdList_delete.size()];
			for (int i = 0; i <hdList_delete.size() ; i++) {
				hdId_delete[i] = hdList_delete.get(i).getId();
				hdId_delete_long[i] = (long)(hdList_delete.get(i).getId());
			}
			hospitalDepartmentMapper.deletes(hdId_delete);//批量删除
//			hospDrAssoMapper.deleteByOfficeCodes(hdId_delete_long);//批量删除科室与医生、医院的关联
		}

		//修改医院与科室关联信息
		if(hdList_update.size()!=0){
			for (int i = 0; i <hdList_update.size() ; i++) {
				HospitalDepartment hd = hdList_update.get(i);
				hd.setUpdatedTime(new Date());
				hd.setHosId(hospId);
				hospitalDepartmentMapper.updateByHospIdDepId(hd);
			}
		}

		//新增医院与科室关联信息
		if(hdList_add.size()!=0){
			for(int i=0;i<hdList_add.size();i++){
				HospitalDepartment hd = hdList_add.get(i);
				hd.setHosId(hospId);
				hd.setCreatedTime(new Date());
				hd.setUpdatedTime(new Date());
				hospitalDepartmentMapper.save(hd);
			}
		}

	}

	@Override
	public List<HospitalDepartment> getHospitalDepartmentByHospId(Long id) {
		HospitalDepartment hd = new HospitalDepartment();
		hd.setHosId(id.intValue());
		return hospitalDepartmentMapper.findByEntityAll(hd);
	}

	@Override
	public int logicalDelete(Long id) {
		return hlHospitalDao.logicalDelete(id);
	}

	@Override
	public HlHospitalEntity queryByHospName(String hospName) {
		return hlHospitalDao.queryByHospName(hospName);
	}

	@Override
	public int logicalDeletes(Long[] ids) {
		for (int i=0; i<ids.length; i++){
			logicalDelete(ids[i]);
		}
		return 0;
	}

	@Override
	public List<HlHospitalEntity> getHospitalListByAreaCode(String areaCode, Integer hosType, String ids) {
		return hlHospitalDao.getHospitalListByAreaCode(areaCode, hosType, ids);
	}

	@Override
	public List<Long> getChildeIds(HlHospitalEntity entity) {
		return hlHospitalDao.getChildeIds(entity);
	}

	@Override
	public List<HlHospitalEntity> getHospitalsByUserId(Long userId) {
		return hlHospitalDao.getHospitalsByUserId(userId);
	}

	@Override
	public void bindWHospital(Long id, String hospitalCode) {
		if (StringUtils.isNotEmpty(hospitalCode)) {
			hlHospitalDao.bindWHospital(id, hospitalCode);
		}
	}

	@Override
	public List<HlHospitalEntity> searchAll() {
		return hlHospitalDao.searchAll();
	}

	/**
	 * 获取子医院id
	 * @return
	 */
	@Override
	public String getChildeIds(){
		String ids = (String) CommonMethod.getSession().getAttribute("ids");
		if ("-1".equals(ids)){
			return null;
		}else if (StringUtils.isNotEmpty(ids)){
			return ids;
		}
		Long userId = ShiroUtils.getUserId();
		ids = "-1";
		SysOrganiEntity organi = sysOrganiService.getOrgani();
		List<HlHospitalEntity> hospitals = getHospitalsByUserId(userId);
		HlHospitalEntity hospital = null;
		if (hospitals!=null&&hospitals.size()>0) {//用户没有医院
			hospital = hospitals.get(0);
		}else {
			hospital = new HlHospitalEntity();
		}

		if (organi!=null) {//用户有绑定机构,以绑定机构为准
			hospital.setOrganiId(organi.getId());
		}
		List<Long> longs = getChildeIds(hospital);
		ids = Joiner.on(",").join(longs);

		CommonMethod.getSession().setAttribute("ids", ids);

		if ("-1".equals(ids)){
			return null;
		}

		return ids;
	}

	/**
	 * 获取慈溪新浦医院子医院id
	 * @return
	 */
	@Override
	public String getCXChildeIds(){
		Long userId = Long.valueOf(1111317);
		String ids = "-1";
		SysOrganiEntity organi = sysOrganiService.getOrgani();
		List<HlHospitalEntity> hospitals = getHospitalsByUserId(userId);
		HlHospitalEntity hospital = null;
		if (hospitals!=null&&hospitals.size()>0) {//用户没有医院
			hospital = hospitals.get(0);
		}else {
			hospital = new HlHospitalEntity();
		}

		if (organi!=null) {//用户有绑定机构,以绑定机构为准
			hospital.setOrganiId(organi.getId());
		}
		List<Long> longs = getChildeIds(hospital);
		ids = Joiner.on(",").join(longs);

		CommonMethod.getSession().setAttribute("ids", ids);

		if ("-1".equals(ids)){
			return null;
		}

		return ids;
	}

	/**
	 * 获取本医院id
	 * @return
	 */
	@Override
	public Long getCurrentHospitalId(){
		Long userId = ShiroUtils.getUserId();
		List<HlHospitalEntity> hospitals = getHospitalsByUserId(userId);
		HlHospitalEntity hospital = null;
		if (hospitals!=null&&hospitals.size()>0) {//用户没有医院
			hospital = hospitals.get(0);
		}else {
			hospital = new HlHospitalEntity();
		}

		return hospital.getId();
	}

	@Override
	public List<HlHospitalEntity> getChildesOne(Long organiId) {
		return hlHospitalDao.getChildesOne(organiId);
	}

}
